package com.xhkj.odps;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.aliyun.odps.Odps;
import com.aliyun.odps.PartitionSpec;
import com.aliyun.odps.account.Account;
import com.aliyun.odps.account.AliyunAccount;
import com.aliyun.odps.data.Record;
import com.aliyun.odps.data.RecordWriter;
import com.aliyun.odps.tunnel.TableTunnel;
import com.aliyun.odps.tunnel.TableTunnel.UploadSession;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hikvision.artemis.sdk.kafka.entity.parse.result.FaceLibData;
import com.hikvision.artemis.sdk.kafka.entity.parse.result.FaceListData;
import com.xhkj.model.MyFaceLibRecogData;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class UploadFaceThread extends Thread {
	private static String accessId = "WB5ePNSftUOLzPoZ";
    private static String accessKey = "hUezl1X21Y7o6b6t9NK4VGz5FFzXAc";
    private static String odpsUrl = "http://service.cn-hs-hsgay-d01.odps.ops.hsgay/api";
    private static String tunnelUrl = "http://dt.cn-hs-hsgay-d01.odps.ops.hsgay";
    private static String partition = "dt";
    private static String project = "hengshuigongan";
    private static String table = "std_hkws_face";
    private static ObjectMapper mapper = new ObjectMapper();
	
    private List<MyFaceLibRecogData> list;
	public UploadFaceThread(List<MyFaceLibRecogData> listByState) {
		this.list = listByState;
	}

	@Override
	public void run() {
		Account account = new AliyunAccount(accessId, accessKey);
		Odps odps = new Odps(account);
		odps.setEndpoint(odpsUrl);
		odps.setDefaultProject(project);
		TableTunnel tunnel = new TableTunnel(odps);
		tunnel.setEndpoint(tunnelUrl);
		PartitionSpec partitionSpec = new PartitionSpec();
		Date date = new Date();
		DateFormat format = new SimpleDateFormat("yyyyMMdd");
		String now = format.format(date);
		partitionSpec.set(partition, now);
		try {
			UploadSession session = tunnel.createUploadSession(project, table, partitionSpec);
			RecordWriter recordWriter = session.openBufferedWriter();
			
			for(MyFaceLibRecogData data:list) {
				Record record = session.newRecord();
				try {
					if (data != null) {
						getRecord(record, "facelibrecoglist_", data);
					}
//					if (data != null && data.getFaceLibRecogList() != null && data.getFaceLibRecogList().size() > 0) {
//						getRecord(record, "facelibrecoglist_", data.getFaceLibRecogList().get(0));
//					}
//					if (data != null && data.getFaceLibNoRecogList() != null && data.getFaceLibNoRecogList().size() > 0) {
//						getRecord(record, "facelibnorecoglist_", data.getFaceLibRecogList().get(0));
//					}
					recordWriter.write(record);
//					log.info("face data upload success" + new Date());
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					log.error("data error：" + mapper.writeValueAsString(data));
					continue;
				}
			}
			recordWriter.close();
			session.commit();
			log.info("ali upload face data success" + this.getName());
		} catch (Exception e) {
			log.error("ali upload face error", e);	
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private static void getRecord(Record record, String qz, MyFaceLibRecogData data) {
		record.set(qz + "traceuuid", data.getTraceUuid());
		record.set(qz + "traceidx", data.getTraceIdx());
		record.set(qz + "cameraindexcode", data.getCameraIndexCode());
		record.set(qz + "deviceindexcode", data.getDeviceIndexCode());
		record.set(qz + "devicename", data.getDeviceName());
		record.set(qz + "plateno", data.getPlateNo());
		record.set(qz + "longitude", data.getLongitude());
		record.set(qz + "latitude", data.getLatitude());
		record.set(qz + "name", data.getName());
		record.set(qz + "certificatenumber", data.getCertificateNumber());
		record.set(qz + "gender", data.getGender());
		record.set(qz + "gendername", data.getGenderName());
		record.set(qz + "age", data.getAge());
		record.set(qz + "agegroup", data.getAgeGroup());
		record.set(qz + "agegroupname", data.getAgeGroupName());
		record.set(qz + "borntime", data.getBornTime());
		record.set(qz + "address", data.getAddress());
		record.set(qz + "glass", data.getGlass());
		record.set(qz + "glassname", data.getGlassName());
		record.set(qz + "smile", data.getSmile());
		record.set(qz + "smilename", data.getSmileName());
		record.set(qz + "facepicurl", data.getFacePicUrl());
		record.set(qz + "bkgurl", data.getBkgUrl());
		record.set(qz + "facerect", data.getFaceRect());
		record.set(qz + "linkfacebodyid", data.getLinkFaceBodyId());
		record.set(qz + "linkfacevehicleid", data.getLinkFaceVehicleId());
		record.set(qz + "capturetime", data.getCaptureTime());
		record.set(qz + "modeldata", data.getModelData());
		record.set(qz + "humanid", data.getHumanId());
	}
}